
//  VistorLoginView.swift
//  LDMicroblog07
//
//  Created by pro on 15/9/16.
//  Copyright © 2015年 pro. All rights reserved.
//

import UIKit

protocol VistorLoginViewDelegate: NSObjectProtocol {

    func VistorLoginBtnClick()
    func VistorRegisterBtnClick()
}




class VistorLoginView: UIView {
    
    ///  代理
    weak var delegate: VistorLoginViewDelegate?
    
    //纯代码 会调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }

    //使用xib 会调用
    required init(coder aDecoder: NSCoder) {
       
        super.init(coder: aDecoder)
        fatalError("init(coder:) has not been implemented")

    }
    //设置主图 信息
    func setupViewInfo(imgName: String, descStr: String, isHidden: Bool) {

        if isHidden == false { // 首页
        
            homeIconView.hidden = false
            iconView.image = UIImage(named: imgName)
            
            setAnimForIconView()
            
        }else {
        
            homeIconView.hidden = true
            iconView.image = UIImage(named: imgName)
            //遮罩前移
            sendSubviewToBack(maskIconView)
        }
        
        descLabel.text = descStr
        
    }
    
    // 添加动画
    private func setAnimForIconView() {
    
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        ani.toValue = 2 * M_PI
        ani.duration = 20.0
        ani.repeatCount = MAXFLOAT
        ani.removedOnCompletion = false
        iconView.layer.addAnimation(ani, forKey: "hhh")
    }
    
    //搭建界面
    private func setupUI() {
    
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(descLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //使用自动布局 frame会失效
        autolayoutViews()
        
    }
    
    private func autolayoutViews() {
    
        //使用自动布局 frame会失效
        ///  iconView
        iconView.translatesAutoresizingMaskIntoConstraints = false
        // CenterX
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        // CenterY
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -40))
        
        ///  homeIconView
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        // CenterX
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        // CenterY
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        ///  descLabel
        descLabel.translatesAutoresizingMaskIntoConstraints = false
        // CenterX
        addConstraint(NSLayoutConstraint(item: descLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        // Top
        addConstraint(NSLayoutConstraint(item: descLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        // vlf 可视化格式
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(240)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView": descLabel]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subView(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView": descLabel]))
        
        // loginBtn
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        // Left
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: descLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        // Top
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: descLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        // width
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subview(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview":loginBtn]))
        // height
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subview(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview": loginBtn]))
        
        // registerBtn
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        // right
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: descLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        // top
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: descLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        // width
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subview(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview": registerBtn]))
        // height
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subview(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview": registerBtn]))
        
        // maskIconView
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        // 水平方向
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subview]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview": maskIconView]))
        // 垂直方向
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subview]-160-|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview": maskIconView]))
        // 设置背景色 与遮罩颜色一致
        backgroundColor = UIColor(white: 237.0/255, alpha: 1.0)
    }
    
    //MARK:-
    func loginBtnClick() {
    
        print(__FUNCTION__)
        delegate?.VistorLoginBtnClick()
    }
    
    func registerBtnClick() {
    
        print(__FUNCTION__)
        delegate?.VistorRegisterBtnClick()
    }
    
    //MARK: - 懒加载 各个子控件
    /// 大图
    lazy var iconView: UIImageView = {
    
        let img = UIImage(named: "visitordiscover_feed_image_smallicon")
        return UIImageView(image: img)
    }()
    ///  遮罩
    lazy var maskIconView: UIImageView = {
   
        let img = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return UIImageView(image: img)
    }()
    ///  小图
    lazy var homeIconView: UIImageView = {
    
        let img = UIImage(named: "visitordiscover_feed_image_house")
        return UIImageView(image: img)
    }()
    /// 文字描述
    lazy var descLabel: UILabel = {
    
        let label = UILabel(frame: CGRectMake(0, 100, 240, 40))
        label.text = "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过"
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 2
        label.textAlignment = NSTextAlignment.Center
        return label
    }()
    /// 登录按钮
    lazy var loginBtn: UIButton = {
    
        let btn = UIButton(type: UIButtonType.Custom)
        btn.frame = CGRectMake(0, 100, 80, 40)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    /// 注册按钮
    lazy var registerBtn: UIButton = {
    
        let btn = UIButton(type: UIButtonType.Custom)
        btn.frame = CGRectMake(100, 100, 80, 40)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "registerBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
}
